package org.example.controller;

import org.example.entity.CustomerRepayPlan;
import org.example.service.CustomerRepayPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * 客户还款计划控制器
 */
@RestController
@RequestMapping("/api/customer-repay-plan")
public class CustomerRepayPlanController {
    
    @Autowired
    private CustomerRepayPlanService customerRepayPlanService;
    
    /**
     * 获取所有还款计划
     */
    @GetMapping("/list")
    public List<CustomerRepayPlan> getAllRepayPlans() {
        return customerRepayPlanService.list();
    }
    
    /**
     * 根据ID获取还款计划
     */
    @GetMapping("/{id}")
    public CustomerRepayPlan getRepayPlanById(@PathVariable Long id) {
        return customerRepayPlanService.getById(id);
    }
    
    /**
     * 根据订单号查询还款计划
     */
    @GetMapping("/search/order-no")
    public List<CustomerRepayPlan> getRepayPlansByOrderNo(@RequestParam String orderNo) {
        return customerRepayPlanService.getByOrderNo(orderNo);
    }
    
    /**
     * 根据账单ID查询还款计划
     */
    @GetMapping("/search/bill-id")
    public CustomerRepayPlan getRepayPlanByBillId(@RequestParam String billId) {
        return customerRepayPlanService.getByBillId(billId);
    }
    
    /**
     * 根据还款状态查询还款计划
     */
    @GetMapping("/search/repay-status")
    public List<CustomerRepayPlan> getRepayPlansByStatus(@RequestParam String repayStatus) {
        return customerRepayPlanService.getByRepayStatus(repayStatus);
    }
    
    /**
     * 根据逾期天数范围查询还款计划
     */
    @GetMapping("/search/overdue-days")
    public List<CustomerRepayPlan> getRepayPlansByOverdueDaysRange(
            @RequestParam(required = false) Integer minDays,
            @RequestParam(required = false) Integer maxDays) {
        return customerRepayPlanService.getByOverdueDaysRange(minDays, maxDays);
    }
    
    /**
     * 根据到期日期范围查询还款计划
     */
    @GetMapping("/search/due-date")
    public List<CustomerRepayPlan> getRepayPlansByDueDateRange(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        LocalDate start = startDate != null ? LocalDate.parse(startDate) : null;
        LocalDate end = endDate != null ? LocalDate.parse(endDate) : null;
        return customerRepayPlanService.getByDueDateRange(start, end);
    }
    
    /**
     * 根据实际还款日期范围查询还款计划
     */
    @GetMapping("/search/actual-repay-date")
    public List<CustomerRepayPlan> getRepayPlansByActualRepayDateRange(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        LocalDate start = startDate != null ? LocalDate.parse(startDate) : null;
        LocalDate end = endDate != null ? LocalDate.parse(endDate) : null;
        return customerRepayPlanService.getByActualRepayDateRange(start, end);
    }
    
    /**
     * 查询逾期还款计划
     */
    @GetMapping("/search/overdue")
    public List<CustomerRepayPlan> getOverduePlans() {
        return customerRepayPlanService.getOverduePlans();
    }
    
    /**
     * 统计逾期金额
     */
    @GetMapping("/statistics/overdue-amount")
    public BigDecimal getOverdueAmount() {
        return customerRepayPlanService.getOverdueAmount();
    }
    
    /**
     * 分页查询还款计划
     */
    @GetMapping("/page")
    public List<CustomerRepayPlan> getRepayPlansPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return customerRepayPlanService.list();
    }
    
    /**
     * 创建还款计划
     */
    @PostMapping
    public boolean createRepayPlan(@RequestBody CustomerRepayPlan repayPlan) {
        return customerRepayPlanService.save(repayPlan);
    }
    
    /**
     * 更新还款计划
     */
    @PutMapping
    public boolean updateRepayPlan(@RequestBody CustomerRepayPlan repayPlan) {
        return customerRepayPlanService.updateById(repayPlan);
    }
    
    /**
     * 删除还款计划
     */
    @DeleteMapping("/{id}")
    public boolean deleteRepayPlan(@PathVariable Long id) {
        return customerRepayPlanService.removeById(id);
    }
}
